﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using YLPublicService.Extensions;
using System.Data.Common;
using System.Data;

using YLPublicService.TransferModel;
using YLPublicService.EnumExtension;

namespace YLPublicService
{


    public partial class AdminService
    {
        public bool UpdateWorkItem(int workItemID, TransferModel.WorkItemStatus status, Guid guid)
        {
            if (!isOnline(guid))
            {
                return false;
            }
            var workItems = from wi in dbContext.WorkItemV2s
                            where wi.ItemID == workItemID
                            select wi;
            if (workItems.Count() > 0)
            {
                var wi = workItems.First();
                wi.Status = status.GetIndex();
                dbContext.SubmitChanges();
            }
            return true;
        }
        public void DeleteWorkItems(string userLoginName, Guid guid)
        {
            //if (!isOnline(guid))
            //    return;
            //var items = from u in dbContext.Users
            //            where u.LoginName == userLoginName
            //            join ui in dbContext.UsersItems on u.UserID equals ui.UserID
            //            join item in dbContext.WorkItems on ui.ItemID equals item.ItemID
            //            select item;

            //foreach (var item in items)
            //{
            //    settingManager.DeleteWorkItemDir(userLoginName, item.boyName, item.grilName);
            //}

            //dbContext.WorkItems.DeleteAllOnSubmit(items);
            //dbContext.SubmitChanges();
            throw new NotImplementedException();
        }

        public void DeleteWorkItem(string userLoginName, string boyName, string grilName, Guid guid)
        {
            //if (!isOnline(guid))
            //    return;
            //var item = (from i in dbContext.WorkItems
            //            where i.boyName == boyName && i.grilName == grilName
            //            select i).First();
            //TransferModel.Template.FromXML(
            //var dirName =getWorkItemServerDirName(
            //settingManager.DeleteWorkItemDir(userLoginName, boyName, grilName);
            //dbContext.WorkItems.DeleteOnSubmit(item);
            //dbContext.SubmitChanges();
            throw new NotImplementedException();
        }

        /// <summary>
        /// 获取等待制作的工作项
        /// </summary>
        /// <param name="userLoginName"></param>
        /// <param name="guid"></param>
        /// <returns></returns>
        public IEnumerable<TransferModel.WorkItem2ServerUser> GetWaitingProcessWorkItems(string userLoginName, Guid guid)
        {
            if (isOnline(guid))
            {
                var users = dbContext.Users.Where(u => u.LoginName == userLoginName);
                if (users.Count() < 1)
                {
                    return null;
                }
                var user = users.First();
                var workitems = getWorkItems<WorkItem2ServerUser>(
                      ui =>
                          ui.UserID == user.UserID && ui.Status == TransferModel.WorkItemStatus.WaitingProcess.GetIndex()
                          ,
                      ui => buildWorkItems2Server(ui)
                    );

                return workitems.ToList();
            }
            return null;
        }
        /// <summary>
        /// 获取用户所有工作项
        /// </summary>
        /// <param name="userLoginName"></param>
        /// <param name="guid"></param>
        /// <returns></returns>
        public IEnumerable<TransferModel.WorkItem2ServerUser> GetWorkItems(string userLoginName, Guid guid)
        {
            if (isOnline(guid))
            {
                var users = dbContext.Users.Where(u => u.LoginName == userLoginName);
                if (users.Count() < 1)
                {
                    return null;
                }
                var user = users.First();
                var workItems = getWorkItems<WorkItem2ServerUser>(
                      ui => ui.UserID == user.UserID,
                      ui => buildWorkItems2Server(ui)
                      );
                return workItems;
            }
            return null;
        }

        private TransferModel.WorkItem2ServerUser buildWorkItems2Server(View_UserAndItem ui)
        {
            var templateItem = dbContext.TemplatesV2s.Where(
                          template => template.TemplateID == ui.TemplateID)
                          .Select(template =>
                              new
                              {
                                  AttachmentsDir = template.AttachmentsDir,
                                  Detial = TemplateItem.TemplateDetial.FromXML(template.Detial)
                              }
                              ).First();
            return new TransferModel.WorkItem2ServerUser
            {
                Detial = ui.Detial,
                WorkItemID = ui.ItemID,
                TemplateID = ui.TemplateID,
                Status = ui.Status.ToEnum<TransferModel.WorkItemStatus>(),
                DBTime = ui.DBTime,
                //PhotoServerRelativePath 是\2013-03-16\...  所以要加上用户的登录名
                FilesRelativeFolder = global::Common.conbindPath(ui.LoginName, ui.PhotoServerRelativePath),
                TemplateAttachmentsFolder = templateItem.AttachmentsDir,
                TemplateName = templateItem.Detial.mName
            };
        }
    }
}